<template>
    <div v-show="show" class="ws-loading">
        <div v-if="lockScroll" class="ws-loading-mask" @touchmove.prevent></div>
        <div class="ws-loading-body">
            <div class="ws-loading-body_animation">
                <div class="sk-wave">
                    <div class="sk-wave-rect"></div>
                    <div class="sk-wave-rect"></div>
                    <div class="sk-wave-rect"></div>
                    <div class="sk-wave-rect"></div>
                    <div class="sk-wave-rect"></div>
                </div>
            </div>
            <div class="ws-loading-body_text">{{ title }}</div>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            show: false,
            title: "加载中...",
            duration: 3000,
            lockScroll: false
        };
    },
    methods: {
        showLoading(option = {}) {
            this.title = option.title || "加载中...";
            this.lockScroll = option.lockScroll || false;
            this.show = true;
            if (option.duration) {
                this.duration = option.duration;
                this.initTimer();
            }
        },
        hideLoading() {
            this.show = false;
        },
        initTimer() {
            setTimeout(() => {
                this.hideLoading();
            }, this.duration);
        }
    }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
.ws-loading {
    position: fixed;
    z-index: 2020;
    .ws-loading-mask {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 2020;
    }
    .ws-loading-body {
        position: fixed;
        z-index: 2021;
        top: 50%;
        left: 50%;
        transform: translateX(-50%) translateY(-50%);
        min-width: 160px;
        padding: 25px 10px 25px 10px;
        border-radius: 16px;
        background: rgb(0, 0, 0);
        opacity: 0.7;

        .ws-loading-body_animation {
            display: flex;
            justify-content: center;
        }

        .ws-loading-body_text {
            text-align: center;
            color: #fff;
            font-size: 28px;
            padding: 20px 15px 0 15px;
            max-width: 220px;
        }
    }

    /* 第三方动画 */
    .sk-wave {
        width: 70px;
        height: 70px;
        display: flex;
        justify-content: space-between;
    }

    .sk-wave-rect {
        background-color: #fff;
        height: 100%;
        width: 10%;
        animation: sk-wave 1.2s infinite ease-in-out;
    }

    .sk-wave-rect:nth-child(1) {
        animation-delay: -1.2s;
    }
    .sk-wave-rect:nth-child(2) {
        animation-delay: -1.1s;
    }
    .sk-wave-rect:nth-child(3) {
        animation-delay: -1s;
    }
    .sk-wave-rect:nth-child(4) {
        animation-delay: -0.9s;
    }
    .sk-wave-rect:nth-child(5) {
        animation-delay: -0.8s;
    }

    @keyframes sk-wave {
        0%,
        40%,
        100% {
            transform: scaleY(0.4);
        }
        20% {
            transform: scaleY(1);
        }
    }
}
</style>